package com.codeyourlife.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeyourlife.entity.Lesson;
import com.codeyourlife.entity.User;
import com.codeyourlife.entity.UserFavCourse;
import com.codeyourlife.entity.UserLearning;
import com.codeyourlife.service.CourseService;
import com.codeyourlife.service.UserRelatedCourseService;
import com.codeyourlife.service.UserService;

import net.sf.json.JSONArray;

@Controller
public class CourseController {

	@Autowired
	private CourseService courseService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserRelatedCourseService userRelatedCourseService;

	@RequestMapping(value = "/getCourse", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getCourse(HttpServletRequest request) {
		String director = request.getParameter("director");
		String type = request.getParameter("type");
		String degree = request.getParameter("degree");
		int intOrderstyle = Integer.parseInt(request.getParameter("orderstyle"));
		int page = Integer.parseInt(request.getParameter("page"));

		String strOrderstyle = "";
		if (intOrderstyle == 1) {
			strOrderstyle = "add_time";
		}
		if (intOrderstyle == 0) {
			strOrderstyle = "click_nums";
		}
		System.out.println(director + " " + type + " " + degree + " " + strOrderstyle + "  " + page);
		try {
			// List<Course> courses =
			// courseService.findCourseByEle(director,type,degree,strOrderstyle,page);
			List<Object> courses = courseService.findCourseByEle(director, type, degree, strOrderstyle, page);
			JSONArray json = JSONArray.fromObject(courses);
			String str = json.toString();
			System.out.println(str);
			return str;
		} catch (Exception e) {
			System.out.println("CourseController错误: ");
		}
		return null;
	}

	@RequestMapping(value = "/getCourseInfo" , produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getCourseInfo(HttpServletRequest request, HttpSession session) {

		int courseId = Integer.parseInt(request.getParameter("courseId"));
		String email = new String();
		email = request.getParameter("email");
		System.out.println(courseId + "  " + email);

		List<Object> courseInfo = null;
		if (!email.equals("undefined")) {
			User user = userService.findByName(email);
			courseInfo = courseService.courseInfo(user.getId(), courseId);
		} else {
			courseInfo = courseService.courseInfo(-1, courseId);
		}
		System.out.println(courseInfo);
		JSONArray json = JSONArray.fromObject(courseInfo);
		String str = json.toString();
		System.out.println(str);
		return str;
		
		/*return courseInfo;*/
	}

	/* } */

	// get Lesson info
	@RequestMapping(value = "/getLesson")
	@ResponseBody
	public List<Lesson> getLesson(HttpServletRequest request) {
		int course_id = Integer.parseInt(request.getParameter("courseId"));
		// int course_id = 13;
		List<Lesson> LessonInfo = courseService.findCourseInfo(course_id);
		System.out.println(LessonInfo);
		return LessonInfo;
	}

	// Update User to Course
	@RequestMapping(value = "/updateStatus")
	@ResponseBody
	public String updateStatus(HttpServletRequest request) {
		String email = request.getParameter("email");
		int u_id = userService.findByName(email).getId();
		int course_id = Integer.parseInt(request.getParameter("courseId"));
		int attend = -1;
		int collect = -1;
		try {
			attend = Integer.parseInt(request.getParameter("attend"));
		} catch (Exception e) {

		}
		try {
			collect = Integer.parseInt(request.getParameter("collect"));
		} catch (Exception e) {

		}
		if (attend == 1) {// attend course
			try {
				UserLearning uLearning = new UserLearning();
				uLearning.setAdd_time(new Timestamp(System.currentTimeMillis()));
				uLearning.setCourse_id(course_id);
				uLearning.setLast_time(new Timestamp(System.currentTimeMillis()));
				uLearning.setUser_id(u_id);
				userRelatedCourseService.addUserLearning(uLearning);
			} catch (Exception e) {
				return "{\"status\":" + 2 + "}";
			}
		} else if (attend == 0) {// out course
			try {
				UserLearning uLearning = new UserLearning();
				uLearning.setCourse_id(course_id);
				uLearning.setUser_id(u_id);
				userRelatedCourseService.deleteUserLearning(uLearning);
			} catch (Exception e) {
				return "{\"status\":" + 3 + "}";
			}
		}
		if (collect == 1) {// collect course
			try {
				UserFavCourse userFav = new UserFavCourse();
				userFav.setAdd_time(new Timestamp(System.currentTimeMillis()));
				userFav.setFavcourse_id(course_id);
				userFav.setUser_id(u_id);
				userRelatedCourseService.addUserFavCourse(userFav);
			} catch (Exception e) {
				return "{\"status\":" + 4 + "}";
			}
		} else if (collect == 0) {// de-collect course
			try {
				UserFavCourse userFav = new UserFavCourse();
				userFav.setFavcourse_id(course_id);
				userFav.setUser_id(u_id);
				userRelatedCourseService.deleUserFavCourse(userFav);
			} catch (Exception e) {
				return "{\"status\":" + 5 + "}";
			}
		}
		return "{\"status\":" + 1 + "}";
	}

}
